package com.bwie.parkdemo.mvvm.ui.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.bwie.lib_core.action.ActActionField;
import com.bwie.lib_core.mvvm.base.BaseActivity;
import com.bwie.parkdemo.BR;
import com.bwie.parkdemo.R;
import com.bwie.parkdemo.databinding.ActivityApplyparkingBinding;
import com.bwie.parkdemo.mvvm.viewmodel.ApplyParkingViewModel;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

/**
 * 申请车位界面
 * */
public class ApplyParkingActivity extends BaseActivity<ActivityApplyparkingBinding, ApplyParkingViewModel> {

    //Environment.getExternalStorageDirectory()->SD下->外部存储
    //当前路径下的图片，其他应用程序都可修改
    private String imgPath;//拍照保存图片uri路径


    @Override
    public int initLayout() {
        return R.layout.activity_applyparking;
    }

    @Override
    public void initView() {
        vm.parkId = getIntent().getBundleExtra(ActActionField.ACTBUNDLE).getInt(ActActionField.ACTBUNDLE);
        vm.setParkName(getIntent().getBundleExtra(ActActionField.ACTBUNDLE).getString("parkName"));
        //是否是来访预约界面判断
        boolean resultFlag = getIntent().getBundleExtra(ActActionField.ACTBUNDLE).getBoolean(ActActionField.FORRESULT);
        vm.resultFlag = resultFlag;
        v.setClicklistener(vm.clickListener);
    }

    @Override
    public int initVariable() {
        return BR.vm;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //初始化当前保存图片路径公共部分->内部存储路径rom(手机系统中路径)->data/data/包名
        imgPath = getExternalCacheDir().getAbsolutePath();
        //绑定接收相机事件
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //解除接收开启相机事件
        EventBus.getDefault().unregister(this);
    }

    //调用系统相机进行拍照操作->viewmodel发送过来的EventBus事件
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void openCam(String str){
//        imgPath = getExternalCacheDir().getAbsolutePath()+"/"+System.currentTimeMillis()+".png";
//        Intent intent = new Intent();
//        intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
//        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(imgPath)));
//        startActivityForResult(intent,200);
        vm.serveImgPath("/sdcard/timg1.jpg");
    }

    //在来访预约界面进入，添加完成车辆信息后接受，车辆数据回传给AddVisitorActivity
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void carInfo(Map<String,Object> map){
        Log.e("ZXWQ","carInfo");
        Bundle bundle = new Bundle();
        bundle.putInt("carId", (Integer) map.get("carId"));
        bundle.putString("carNum", (String) map.get("carNum"));
        Intent intent = new Intent();
        intent.putExtra(ActActionField.ACTBUNDLE,bundle);
        setResult(RESULT_OK,intent);
        finish();
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //避免一个if多个并列条件
        //避免if多层嵌套
        //多实用return
        if (requestCode != 100)
            return;
        if (resultCode != RESULT_OK)
            return;
        //请求状态为100且用户拍照后点击的为保存
        //将图片路径发送到viewmodel中
        if (requestCode== 200) {
            Log.e("ZXY", "onActivityResult:" + imgPath);
            vm.serveImgPath(imgPath);
        }
    }
}
